/*
 * @Author: your name
 * @Date: 2021-12-02 15:03:29
 * @LastEditTime: 2021-12-07 08:54:48
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \electron-demo\src\utils\request.js
 */
// 请求二次封装
import axios from "axios";
import { useNavigate } from "react-router-dom";
// import qs from "qs";
let baseURL = "/api";


axios.defaults.adapter = require("axios/lib/adapters/http");

// create an axios instance
const service = axios.create({
  baseURL: baseURL,
  timeout: 5000, // request timeout
});

// request interceptor
service.interceptors.request.use(
  (config) => {
    if (config.method == "post") {
      // config.data = qs.stringify(config.data);
    }
    console.log(config);
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  (response) => {
    const navigate = useNavigate();
    const res = response.data;
    console.log(res);
    if (res.code !== 1 && res.code !== 200) {
      if (res.code === 401 || res.code === 403 || res.code === 405) {
        // 重新跳转登录，获取token
      } else if (res.code === 0) {
        if (res.data.statuscode == 401) {
        }
      } else if (res.code === 100) {
        console.log(100);
        return;
      } else if (res.code == 108) {
        console.log(108);
        return;
      }
      return Promise.reject(new Error(res.msg || "Error"));
    } else {
      return res;
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
